<?php
/**
 * @title 463. 岛屿的周长 - BFS
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $grid
     * @return Integer
     */
    function islandPerimeter($grid) {
        /**
         * @since 2020-10-30 周长
         */
        $result = 0;

        /**
         * @since 2020-10-30 宽高
         */
        $w = count($grid[0]);
        $h = count($grid);

        /**
         * @since 2020-10-30 迭代点
         */
        $next = [];

        /**
         * @since 2020-10-30 2层迭代
         */
        for($i=0; $i<$h; $i++){
            /**
             * @since 2020-10-30 横向迭代
             */
            for($j=0; $j<$w; $j++){
                if($grid[$i][$j] == 1){
                    $next[] = [$i, $j];
                    $grid[$i][$j] = 0;
                    $result = 4;

                    /**
                     * @since 2020-10-30 找到1个1, 跳出2层迭代
                     */
                    $i=$h;
                    break;
                } else {}
            }
        }
        // return $next;

        /**
         * @since 2020-10-30 1扩散4个方向查找
         */
        while($next){
            $nextTmp = [];

            /**
             * @since 2020-10-30 哈希
             */
            $hash = [];

            /**
             * @since 2020-10-30 遍历每个1
             */
            foreach ($next as list($n, $m)){
                // echo "n={$n}, m={$m}", PHP_EOL;

                /**
                 * @since 2020-10-30 4个方向, 向上
                 */
                if($n >= 1){
                    // echo "up, ";
                    if(isset($hash[$n-1][$m])){
                        $result -= 2;
                        // echo "-2", PHP_EOL;
                    } elseif($grid[$n-1][$m] == 1) {
                        $hash[$n-1][$m] = 1;
                        $grid[$n-1][$m] = 0;

                        $nextTmp[] = [$n-1, $m];
                        $result += 2;
                        // echo "+2", PHP_EOL;
                    } else {
                        // echo "0", PHP_EOL;
                    }
                } else {}


                /**
                 * @since 2020-10-30 向下
                 */
                if($n <= $h-2){
                    // echo "down, ";
                    if(isset($hash[$n+1][$m])){
                        $result -= 2;
                        // echo "-2", PHP_EOL;
                    } elseif($grid[$n+1][$m] == 1){
                        $hash[$n+1][$m] = 1;
                        $grid[$n+1][$m] = 0;

                        $nextTmp[] = [$n+1, $m];
                        $result += 2;
                        // echo "+2", PHP_EOL;
                    } else {
                        // echo "0", PHP_EOL;
                    }
                } else {}

                /**
                 * @since 2020-10-30 向左
                 */
                if($m >= 1){
                    // echo "left, ";
                    if(isset($hash[$n][$m-1])){
                        $result -= 2;
                        // echo "-2", PHP_EOL;
                    } elseif($grid[$n][$m-1] == 1) {
                        $hash[$n][$m-1] = 1;
                        $grid[$n][$m-1] = 0;

                        $nextTmp[] = [$n, $m-1];
                        $result += 2;
                        // echo "+2", PHP_EOL;
                    } else {
                        // echo "0", PHP_EOL;
                    }
                } else {}

                /**
                 * @since 2020-10-30 向右
                 */
                if($m <= $w-2){
                    // echo "right, ";
                    if(isset($hash[$n][$m+1])){
                        $result -= 2;
                        // echo "-2", PHP_EOL;
                    } elseif($grid[$n][$m+1] == 1) {
                        $hash[$n][$m+1] = 1;
                        $grid[$n][$m+1] = 0;

                        $nextTmp[] = [$n, $m+1];
                        $result += 2;
                        // echo "+2", PHP_EOL;
                    } else {
                        // echo "0", PHP_EOL;
                    }
                } else {}
                // echo PHP_EOL;
            }

            /**
             * @since 2020-10-30 下次迭代
             */
            // echo str_repeat(PHP_EOL, 2);
            $next = $nextTmp;
        }

        /**
         * @since 2020-10-30 周长
         * @return
         */
        return $result;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.30_2.php
 */
$datas = [
    [
        [[0,1,0,0],
            [1,1,1,0],
            [0,1,0,0],
            [1,1,0,0]],
    ], // 16
    [
        [[0],[1]],
    ], // 4
    [
        [[1,1],[1,1]]
    ], // 8
    [
        [[1,0],[0,1]]
    ], // 8

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->islandPerimeter(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
